package com.stu.chart.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.stu.chart.entity.PublichEvaluate;
import com.stu.chart.entity.Staff;
import com.stu.chart.entity.UserPublish;
import com.stu.chart.service.PublichEvaluateService;
import com.stu.chart.service.StaffService;
import com.stu.chart.service.UserPublishService;
import com.stu.chart.util.Rvo;
import com.stu.chart.util.SecurityUtil;
import org.apache.tomcat.util.security.MD5Encoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.parameters.P;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author dsj
 */
@RestController
public class UserPublishController {
    @Autowired
    private UserPublishService userPublishService;
    @Autowired
    private StaffService staffService;
    @Autowired
    private PublichEvaluateService publichEvaluateService;
    @Value("${file.temp}")
    private String filePath;

    @RequestMapping("publish")
    public Rvo updateStaff(@RequestBody UserPublish userPublish){
        userPublish.setPublishTime(new Date());
        userPublish.setUserId(staffService.getUidbyname(SecurityUtil.getUser().getUsername()));
        boolean b = userPublishService.save(userPublish);
        return b?Rvo.ok(): Rvo.error(null);
    }

    @RequestMapping("delPublish")
    public Rvo delPublish(@RequestParam String id){
        boolean b = userPublishService.removeById(id);
        return b?Rvo.ok(): Rvo.error(null);
    }

    @RequestMapping("listPublished")
    public Rvo listPublished(Page page,String title,Integer userId){
        String username = SecurityUtil.getUser().getUsername();
        IPage<UserPublish> list = null;
        Integer uidbyname = staffService.getUidbyname(username);
        list = userPublishService.page(page,Wrappers.lambdaQuery(UserPublish.class)
                .eq(userId != null,UserPublish::getUserId,userId)
                .like(StringUtils.isNotBlank(title), UserPublish::getTitle, title)
                .orderByDesc(UserPublish::getPublishTime));
        list.getRecords().forEach(e->{
            Integer userId1 = e.getUserId();
            Staff byId = staffService.getById(userId1);
            e.setPublisher(byId.getNick());
            e.setPicture(byId.getPicture());
            e.setStatus(byId.getStatus());
            PublichEvaluate one = publichEvaluateService.getOne(Wrappers.lambdaQuery(PublichEvaluate.class)
                    .eq(PublichEvaluate::getEvaluateUserId, uidbyname).eq(PublichEvaluate::getPublishId,e.getId()));
            if(one != null){
                e.setScore(one.getEvaluateNum());
            }
        });
        return Rvo.ok(list);
    }

    @RequestMapping("evaluate")
    public Rvo evaluate(@RequestBody Map<String,Integer> evaluate){
        PublichEvaluate p = new PublichEvaluate();
        p.setEvaluateNum(evaluate.get("number"));
        p.setPublishId(evaluate.get("publishId"));
        p.setEvaluateTime(new Date());
        Integer uidbyname = staffService.getUidbyname(SecurityUtil.getUser().getUsername());
        p.setEvaluateUserId(uidbyname);
        PublichEvaluate one = publichEvaluateService.getOne(Wrappers.lambdaQuery(PublichEvaluate.class)
                .eq(PublichEvaluate::getEvaluateUserId, uidbyname).eq(PublichEvaluate::getPublishId, p.getPublishId()));
        if(one != null){
            one.setEvaluateNum(p.getEvaluateNum());
            publichEvaluateService.updateById(one);
            return Rvo.ok();
        }
        boolean b = publichEvaluateService.save(p);
        return b?Rvo.ok(): Rvo.error(null);
    }

    @RequestMapping("uploadFile")
    public Rvo uploadFile(MultipartFile file) throws IOException {
        String path = UUID.randomUUID() +file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        File file1 = new File(filePath + File.separator + path);
        File parentFile = file1.getParentFile();
        if(!parentFile.exists()){
            parentFile.mkdirs();
        }
        file.transferTo(file1);
        return Rvo.ok(null,"http://106.54.170.35:8080/file/"+path);
    }
}
